package com.shenmazong.servereduuser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shenmazong.servereduuser.entity.TbAdmin;
import com.shenmazong.servereduuser.entity.TbCollage;
import com.shenmazong.servereduuser.entity.TbStudent;
import com.shenmazong.servereduuser.mapper.TbCollageMapper;
import com.shenmazong.servereduuser.mapper.TbStudentMapper;
import com.shenmazong.servereduuser.service.TbCollageService;
import com.shenmazong.servereduuser.service.TbStudentService;
import com.shenmazong.utils.ConstUtils;
import com.shenmazong.utils.Idutils;
import com.shenmazong.utils.JwtUtils;
import com.shenmazong.utils.ResultResponse;
import com.shenmazong.vo.ListParamVo;
import com.shenmazong.vo.LoginSuccessVo;
import com.shenmazong.vo.StudentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 *
 */
@Service
public class TbStudentServiceImpl implements TbStudentService {

    public final static Integer STUDENT_MIN_NO = 100000;


    @Autowired
    TbStudentMapper tbStudentMapper;

    @Autowired
    TbCollageMapper tbCollageMapper;

    private Integer getCollageNo() {
        TbStudent one = tbStudentMapper.selectOne(
                new QueryWrapper<TbStudent>().lambda().orderByDesc(TbStudent::getStuNo).last("limit 1")
        );
        if(one == null) {
            return STUDENT_MIN_NO;
        }

        return one.getStuNo() + 1;
    }

    @Override
    public ResultResponse add(StudentVo student) {

        TbStudent one = tbStudentMapper.selectOne(
                new QueryWrapper<TbStudent>().lambda().eq(TbStudent::getStuIdcard, student.getStuIdcard()).last("limit 1")
        );
        if(one != null) {
            return ResultResponse.FAILED(400, "学生已经存在");
        }

        TbCollage tbCollage = tbCollageMapper.selectOne(
                new QueryWrapper<TbCollage>().lambda().eq(TbCollage::getCollageId, student.getCollageId())
        );
        if(tbCollage == null) {
            return ResultResponse.FAILED(400, "学院不存在");
        }

        //
        TbStudent tbStudent = new TbStudent();
        BeanUtils.copyProperties(student, tbStudent);

        tbStudent.setStuId(Idutils.getId());
        tbStudent.setStuNo(getCollageNo());

        tbStudentMapper.insert(tbStudent);

        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse del(StudentVo student) {
        //--1
        if(student.getStuId() == null || student.getStuId() == 0) {
            return ResultResponse.FAILED(400, "学生ID不正确");
        }

        //--2
        tbStudentMapper.deleteById(student.getStuId());

        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse get(StudentVo student) {
        //--1
        if(student.getStuId() == null || student.getStuId() == 0) {
            return ResultResponse.FAILED(400, "学院ID不正确");
        }

        //--2
        TbStudent tbStudent = tbStudentMapper.selectById(student.getStuId());
        if(tbStudent == null) {
            return ResultResponse.FAILED(404, "信息不存在");
        }

        //--3
        BeanUtils.copyProperties(tbStudent, student);

        return ResultResponse.SUCCESS(student);
    }

    @Override
    public ResultResponse set(StudentVo student) {
        //--1
        if(student.getStuId() == null || student.getStuId() == 0) {
            return ResultResponse.FAILED(400, "学院ID不正确");
        }

        //--2
        TbStudent tbStudent = tbStudentMapper.selectById(student.getStuId());
        if(tbStudent == null) {
            return ResultResponse.FAILED(404, "信息不存在");
        }

        //--3
        BeanUtils.copyProperties(student, tbStudent);
        tbStudentMapper.updateById(tbStudent);

        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse page(ListParamVo param) {
        Page pg = new Page(param.getCurrent(), param.getSize());

        Page page = tbStudentMapper.selectPage(pg, null);

        return ResultResponse.SUCCESS(page);
    }

    @Override
    public ResultResponse login(StudentVo student) {
        //--1 检查参数
        if(student.getStuName() == null) {
            return ResultResponse.FAILED(401, "账号不正确");
        }
        if(student.getStuPass() == null) {
            return ResultResponse.FAILED(401, "密码不正确");
        }

        //--2 验证密码
        TbStudent one = tbStudentMapper.selectOne(
                new QueryWrapper<TbStudent>().lambda()
                        .eq(TbStudent::getStuName, student.getStuName())
                        .last("limit 1")
        );
        if(one == null) {
            return ResultResponse.FAILED(404, "账号不存在");
        }

        //--3
        if(!one.getStuPass().equals(student.getStuPass())) {
            return ResultResponse.FAILED(403, "密码不正确");
        }

        LoginSuccessVo loginSuccessVo = new LoginSuccessVo();
        loginSuccessVo.setUserId(one.getStuId());
        loginSuccessVo.setUserName(one.getStuName());
        loginSuccessVo.setPermissions("");
        loginSuccessVo.setAvatar("https://i.gtimg.cn/club/item/face/img/2/15922_100.gif");

        //生成token
        String token = JwtUtils.makeToken(one.getStuId(), one.getStuName(), ConstUtils.STUDENT_ROLE, one.getRealName());
        loginSuccessVo.setAccessToken(token);
        return ResultResponse.SUCCESS(loginSuccessVo);
    }
}




